package br.com.gvt.webp.bean;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.bean.ManagedBean;

import org.primefaces.event.TabChangeEvent;

import br.com.gvt.webp.dao.InfosServer;
import br.com.gvt.webp.dao.SUnit;
import br.com.gvt.webp.model.Server;

import com.thoughtworks.xstream.XStream;

@ManagedBean(name="serverBean")
public class ServerBean implements Serializable {

	private static final long serialVersionUID = 1L;
	private List<Server> listaServers;
	private String hosts[];
	private int port1, port2, port3, port4;
	private String arquivo;
	private InfosServer infos;
	private String currentTab;
	
	// --------------
	// Constructor
	public ServerBean() {
		currentTab = "DEV1";
		port1 = 7000;
		port2 = 7010;
		port3 = 8000;
		port4 = 8010;

		listaServers = new ArrayList<Server>();
		criaServidores();
		carregaCredenciaisServers();
	}
	
	// ---------------
	// Methods
	public void criaServidores() {
		hosts = new String[12];
		hosts[0] = "svuxdsoa49";
		hosts[1] = "svuxqsoa87";
		hosts[2] = "svuxdsoa68";
		hosts[3] = "svuxdsoa65";
		hosts[4] = "svuxqsoa123";
		hosts[5] = "svuxdsoa10";
		hosts[6] = "svuxqsoa124";
		hosts[7] = "svuxqa1soa54";
		hosts[8] = "svuxqsoa64";
		hosts[9] = "svuxqsoa55";
		hosts[10] = "svuxdsoa49";
		hosts[11] = "svuxqa6soa54";

		listaServers.add(new Server(hosts[0], port1));
		listaServers.add(new Server(hosts[0], port2));
	}
	
	
	public void carregaCredenciaisServers(){
		
		XStream xstream = new XStream();	
		
		try {
			
			FileInputStream file = new FileInputStream(new File("infosServer.xml"));
			xstream.alias("InfosServer", InfosServer.class);
			xstream.alias("SUnit", SUnit.class);
			this.infos = (InfosServer)xstream.fromXML(file);
			file.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	
	public InfosServer getInfos() {
		return infos;
	}

	public void setInfos(InfosServer infos) {
		this.infos = infos;
	}

	public void abreArquivo() throws IOException {
		Runtime.getRuntime().exec("notepad " + arquivo);
	}

	// ------------------
	// Getters and Setters
	public List<Server> getListaServers() {
		return listaServers;
	}
	public void setListaServers(List<Server> listaServers) {
		this.listaServers = listaServers;
	}
	public String getArquivo() {
		return arquivo;
	}
	public void setArquivo(String arquivo) {
		this.arquivo = arquivo;
	}
	public String getCurrentTab() {
		return currentTab;
	}
	public void setCurrentTab(String currentTab) {
		this.currentTab = currentTab;
	}

	public void onTabChange(TabChangeEvent event) {  
        if(event.getTab().getTitle().equals("DEV1")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[0], port1));
    		listaServers.add(new Server(hosts[0], port2));
    		setListaServers(listaServers);
    		
    		setCurrentTab("DEV1");
        }
        else if(event.getTab().getTitle().equals("DEV2")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[1], port1));
    		listaServers.add(new Server(hosts[1], port2));
    		setListaServers(listaServers);
    		
    		setCurrentTab("DEV2");
        }
        else if(event.getTab().getTitle().equals("DEV3")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[2], port1));
    		listaServers.add(new Server(hosts[2], port2));
    		setListaServers(listaServers);
    		
    		setCurrentTab("DEV3");
        }
        else if(event.getTab().getTitle().equals("DEV4")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[3], port1));
    		listaServers.add(new Server(hosts[3], port2));
    		setListaServers(listaServers);
    		
    		setCurrentTab("DEV4");
        }
        else if(event.getTab().getTitle().equals("DEV5")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[4], port1));
    		listaServers.add(new Server(hosts[4], port2));
    		setListaServers(listaServers);
    		
    		setCurrentTab("DEV5");
        }
        else if(event.getTab().getTitle().equals("DEV6")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[5], port1));
    		listaServers.add(new Server(hosts[5], port2));
    		setListaServers(listaServers);
    		
    		setCurrentTab("DEV6");
        }
        else if(event.getTab().getTitle().equals("DEV7")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[6], port1));
    		listaServers.add(new Server(hosts[6], port2));
    		setListaServers(listaServers);
    		
    		setCurrentTab("DEV7");
        }
        else if(event.getTab().getTitle().equals("QA1")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[7], port3));
    		listaServers.add(new Server(hosts[7], port4));
    		setListaServers(listaServers);
    		
    		setCurrentTab("QA1");
        }
        else if(event.getTab().getTitle().equals("QA2")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[8], port3));
    		listaServers.add(new Server(hosts[8], port4));
    		setListaServers(listaServers);
    		
    		setCurrentTab("QA2");
        }
        else if(event.getTab().getTitle().equals("QA3")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[9], port3));
    		listaServers.add(new Server(hosts[9], port4));
    		setListaServers(listaServers);
    		
    		setCurrentTab("QA3");
        }
        else if(event.getTab().getTitle().equals("QA4")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[10], port3));
    		listaServers.add(new Server(hosts[10], port4));
    		setListaServers(listaServers);
    		
    		setCurrentTab("QA4");
        }
        else if(event.getTab().getTitle().equals("QA6")){
        	listaServers = new ArrayList<Server>();
        	listaServers.add(new Server(hosts[11], port3));
    		listaServers.add(new Server(hosts[11], port4));
    		setListaServers(listaServers);
    		
    		setCurrentTab("QA6");
        }
    }
}
